var $table;  
var selectionIds = [];  //保存选中ids 
var toolbar = "";
var dialogTable;
var tableName = "";
$(function () {
	  var columns =[
                    {field: 'state', checkbox: true},
					{field: 'TABLENAME' , title: '表名', sortName:'tableName' , sortable: false,halign: 'center',align: 'center'},
					{field: 'engine' , title: 'Engine', sortName:'engine' , sortable: false,halign: 'center',align: 'center'},
					{field: 'TABLECOMMENT' , title: '表备注', sortName:'tableComment' , sortable: false,halign: 'center',align: 'center'},
					{field: 'createTime' , title: '创建时间  ' , sortName:'status' , sortable: false,halign: 'center',align: 'center'},
			    {field: 'action', title: '操作', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: false, width: 150, halign: 'center', align: 'center'}
	  ];
  $table = new BSTable("table", 'sys/generator/list', columns,"toolbar",560,'TABLENAME','TABLE_NAME','sysrole');
  var queryParams = function(params) {            
       return {
    	   page: params.pageNumber,
     	   limit: params.pageSize,
     	   sidx:params.sortName,
           order: params.sortOrder,
           tableName:vm.search.tableName  
          //查询字段
        };
   }
   $table.setQueryParams(queryParams);
   $table.init();
});


var vm = new Vue({
	el:'#app',
	data:{
		showList: true,
		search:{
			id:null
		}
	},
	methods: {
		reload:function(){
			 defaultActions.reload($("#table"),$("#defaultForm"),vm,"sysUser"); 
		},
		query: function () {
			defaultActions.query();
		},
		generator: function() {
			var tableNames = getSelectRows($('#table'));
			if(tableNames == null){
				return ;
			}
			location.href = $path + "sys/generator/code?tables=" +tableNames.join(',');
		},
		edit: function(event, id){
			  tableName = id;
				var fun = function(dialogRef) {
					
					$.ajax({
								type: "POST",
								    url: $path+"sys/generator/setColumns?tableName="+tableName,
								    data: JSON.stringify(dialogTable.bootstrapTable('getData')),
								    success: function(r){
								    	 dialogRef.close();
												if(r.code == 0){
													  location.href = $path + "sys/generator/codeDetail?tableName="+tableName;
												}else{
													alert(r.msg);
												}
										}
							});
				};
				
	      defaultActions.assiot('', '../dialog/generator_dialog.html', fun,'生成代码');
		
		}
	}
});


actionFormatter = function(value, row, index) {
    	  var button = [];
    	  button.push('<a class="edit ml10" href="javascript:void(0)"  data-toggle="tooltip" title="Edit"><i class="glyphicon glyphicon-edit"></i></a>&nbsp;&nbsp;');
    		return button.join('');
}

window.actionEvents = {    
    'click .edit': function (e, value, row, index) {
    	vm.edit(e,row.id);
    },
    'click .remove': function (e, value, row, index) {
    	vm.del(e,row.id);
    },
    'click .detail': function (e, value, row, index) {
    	vm.detail(e,row.id);
    }
};
